layui.use(['layer', 'form', 'table', 'admin', 'ax', 'func','tree'], function () {
    var $ = layui.$;
    var layer = layui.layer;
    var form = layui.form;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var func = layui.func;
    var tree = layui.tree;

    /**
     * 系统管理--消息管理
     */
    var Emp = {
        tableId: "noticeTable",    //表格id
        condition: {
            deptId: "",
            deptName:""
        }
    };
    /**
     * 检索店名
     */
    /*var languageAjax = new $ax(Feng.ctxPath + "/dept/deptSeles");
    var languageResult = languageAjax.start();
    $.each(languageResult.data, function( index, val ) {
        $("#dm").append("<option value='"+val.deptId+"'>"+val.name+"</option>");
    } );
    form.render('select');*/
    /**
     * 初始化表格的列
     */
    Emp.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', align: "left", hide: true, sort: true, title: 'id'},
            {field: 'bh', align: "left", sort: true, title: '编号',width:180},
            {field: 'xm', align: "left", sort: false, title: '姓名',width:90},
            {field: 'depts', align: "left", sort: true, title: '部门',width:250},
            {field: 'tel', align: "left", sort: false, title: '电话',width: 150},
            {field: 'zw', align: "left", sort: false, title: '职务',width: 70},
            {field: 'rq', align: "left", sort: true, title: '入职日期',width:110},
            // {field: 'yhk', align: "left", sort: false, title: '银行卡',width:180},
            {field: 'qy', align: "left", sort: false, title: '区域',width:110},
            {field: 'eqy', align: "left", sort: false, title: '二级区域',width:120},
            {field: 'bz', align: "left", sort: false, title: '备注'}
            // {field: 'gz', align: "left", sort: false, title: '工资',width:80},
            // {align: 'center', toolbar: '#tableBar', title: '操作', width: 120}
        ]];

    };
    // 重置按钮事件
    $('#btnReset').click(function () {
        $('input').val('');

    });
    /**
     * 点击查询按钮
     */
    Emp.search = function () {
        var queryData = {};
        queryData['xm'] = $("#condition").val();
        queryData['deptId'] = Emp.condition.deptId;
        queryData['tel'] = $("#tel").val();
        table.reload(Emp.tableId, {where: queryData, page: {curr: 1}});
    };

    /**
     * 点击查询按钮
     */
    Emp.onClickDept = function (obj) {
        Emp.condition.deptId = obj.data.id;
        Emp.condition.deptName = obj.data.title;
        $("#condition").val('');
        $("#tel").val('');
        Emp.search();
    };

    /**
     * 弹出添加通知
     */
    Emp.openAddNotice = function () {
        func.open({
            height: 420,
            title: '添加通知',
            content: Feng.ctxPath + '/notice/notice_add',
            tableId: Emp.tableId
        });
    };
    /**
     * 点击删除通知
     *
     * @param data 点击按钮时候的行数据
     */
    Emp.onDeleteNotice = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/notice/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(Emp.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("noticeId", data.noticeId);
            ajax.start();
        };
        Feng.confirm("是否删除通知 " + data.title + "?", operation);
    };
    /**
     * 点击修改档案
     *
     * @param data 点击按钮时候的行数据
     */
    Emp.onEditNotice = function (data) {
        func.open({
            height: 920,
            title: '修改档案',
            content: Feng.ctxPath + "/btyArchives/btyArchives_update/" + data.id,
            tableId: Emp.tableId
        });
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + Emp.tableId,
        url: Feng.ctxPath + '/emp/list',
        page: true,
        even: true,
        cellMinWidth: 100,
        limit: 20,
        height: "full-64",
        cols: Emp.initColumn(),
    });

    //初始化左侧部门树
    var ajax = new $ax(Feng.ctxPath + "/dept/layuiTree", function (data) {
        tree.render({
            elem: '#deptTree',
            data: data,
            click: Emp.onClickDept,
            onlyIconControl: true
        });
    }, function (data) {
    });
    ajax.start();

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        Emp.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        Emp.openAddNotice();
    });

    //同步员工信息
    $("#btnSyncUser").click(function () {
        var l_index;
        $.ajax({
            url: Feng.ctxPath + "/dept/syncUserFromDingDing",
            cache: false,
            dataType:"json",
            data: {"deptId":Emp.condition.deptId},
            timeout: 600*1000,//10min
            beforeSend: function () {
                l_index = layer.load(1);
            },
            success: function (data) {
                if (data.code != 200) {
                    Feng.error(data.message);
                }else {
                    if(data.data!=""){
                        Feng.success(data.data);
                    }
                    Emp.search();
                }
                layer.close(l_index);
            },
            error: function (data) {
                layer.alert('同步失败！');
                layer.close(l_index);
            }
        });
    });
    //生成用户数据
    $("#btnGenUser").click(function () {
        var tip='';
        var checkStatus = table.checkStatus(Emp.tableId);
        var nums = checkStatus.data.length; //获取选中行数量
        var deptName=Emp.condition.deptName;
        var deptId=Emp.condition.deptId;
        var param={};
        if(nums!=0){
            tip="是否生成选中员工的用户信息？";
            var empIds=[];
            checkStatus.data.forEach(function (item,i) {
                empIds.push(item.id);
            });
            param.type="emp";//根据员工生成
            param.empIds=empIds.join(",");
        }else if(deptId!=0){
            tip="是否生成部门’ "+deptName+" ‘下的员工用户信息？";
            param.type="dept";//根据部门生成
            param.deptId=deptId;
        }else {
            Feng.error("请选择数据");
            return;
        }

        layer.confirm(tip, {icon: 3, title:'确认'}, function(index){
            var l_index;
            $.ajax({
                url: Feng.ctxPath + "/emp/generateUserFromEmp",
                cache: false,
                dataType:"json",
                data: param,
                timeout: 600*1000,//10min
                beforeSend: function () {
                    l_index = layer.load(1);
                },
                success: function (data) {
                    if (!data.success) {
                        Feng.error(data.message);
                    }else {
                        if(data.data!=""){
                            Feng.success("生成"+data.data+"个新用户");
                        }
                    }
                    layer.close(l_index);
                },
                error: function (data) {
                    layer.alert('生成用户失败！');
                    layer.close(l_index);
                }
            });
            layer.close(index);
        });


    });


    // 工具条点击事件
    table.on('tool(' + Emp.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            Emp.onEditNotice(data);
        } else if (layEvent === 'delete') {
            Emp.onDeleteNotice(data);
        }
    });
});
$(function () {
    var panehHidden = false;
    if ($(this).width() < 769) {
        panehHidden = true;
    }
    $('#myContiner').layout({initClosed: panehHidden, west__size: 260});
});